package net.osc.web.job;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.osc.model.job.CommonJobDetail;
import net.osc.service.job.CommonJobManager;
import net.osc.web.BaseController;
import net.osc.web.JsonResult;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
public class CommonJobController extends BaseController {
	
	@Resource
	private CommonJobManager commonJobManager;

	public CommonJobManager getCommonJobManager() {
		return commonJobManager;
	}

	public void setCommonJobManager(CommonJobManager commonJobManager) {
		this.commonJobManager = commonJobManager;
	}

	@RequestMapping("/**/job/delJobs.do")
	@ResponseBody
	public Object delJobs(HttpServletRequest req, Long[] ids) {
		commonJobManager.delJobs(ids);
		return new JsonResult(true, "");
	}

	@RequestMapping("/**/job/saveJob.do")
	@ResponseBody
	public Object saveJob(HttpServletRequest req, CommonJobDetail job) {
		try {
			commonJobManager.saveJob(job);
		} catch (Exception e) {
			return new JsonResult(false, e.getMessage());
		}
		return new JsonResult(true, "");
	}

	@RequestMapping("/**/job/activeJobs.do")
	@ResponseBody
	public Object activeJob(HttpServletRequest req, Long[] ids) {
		for (Long id : ids) {
			CommonJobDetail job = commonJobManager.getJobById(id);
			try {
				commonJobManager.activeJob(job);
			} catch (Exception e) {
				return new JsonResult(false, e.getMessage());
			}
		}
		return new JsonResult(true, "");
	}

	@RequestMapping("/**/job/disActiveJobs.do")
	@ResponseBody
	public Object disActiveJob(HttpServletRequest req, Long[] ids) {
		for (Long id : ids) {
			CommonJobDetail job = commonJobManager.getJobById(id);
			commonJobManager.disActiveJob(job);
		}
		return new JsonResult(true, "");
	}

}
